#include "gamekit/drawing/Color.h"

#include <math.h>

using namespace gamekit;

Color::Color()
	: m_r(0),
	m_g(0),
	m_b(0),
	m_a(0)
{

}

Color::Color(const unsigned char& r, const unsigned char& g, const unsigned char& b) :
	m_r(r),
	m_g(g),
	m_b(b),
	m_a(255)
{
}

Color::Color(const unsigned char& r, const unsigned char& g, const unsigned char& b, const unsigned char& a) :
	m_r(r),
	m_g(g),
	m_b(b),
	m_a(a)
{
}

Color::~Color(void)
{
}

unsigned char Color::r(void) const
{
	return m_r;
}

void Color::setR(const unsigned char& r)
{
	m_r = r;
}

unsigned char Color::g(void) const
{
	return m_g;
}

void Color::setG(const unsigned char& g)
{
	m_g = g;
}

unsigned char Color::b(void) const
{
	return m_b;
}

void Color::setB(const unsigned char& b)
{
	m_b = b;
}

unsigned char Color::a(void) const
{
	return m_a;
}

void Color::setA(const unsigned char& a)
{
	m_a = a;
}

Color Color::operator =(const Color& color)
{
	m_a = color.a();
	m_r = color.r();
	m_g = color.g();
	m_b = color.b();

	return Color(color);
}

Color Color::alphaBlend(const Color& bg, const Color& color)
{
	double da = bg.a() / 255.0;
    double sa = color.a() / 255.0;

    double ax = sa + da * (1.0 - sa);
    double rx = (color.r() * sa + bg.r() * da * (1.0 - sa)) / ax;
    double gx = (color.g() * sa + bg.g() * da * (1.0 - sa)) / ax;
    double bx = (color.b() * sa + bg.b() * da * (1.0 - sa)) / ax;

	ax = ceil(ax * 255.0);

	return Color(rx, gx, bx, ax);
}
